
#import "RCTEntryViewController.h"

#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import "JZEClassPlayerController.h"
#import "ECourseConstant.h"

#import <React/RCTBridge.h>
#import <React/RCTEventDispatcher.h>

@interface RCTEntryViewController ()
@property (nonatomic, strong) RCTBridge *bridge;
@end

/**
 打离线包
 react-native bundle --entry-file index.ios.js --bundle-output ./ios/bundle/index.ios.jsbundle --platform ios --assets-dest ./ios/bundle --dev false
 
 com.orangenet.Http2Test
 */

@implementation RCTEntryViewController

@synthesize bridge = _bridge;

- (void)viewDidLoad {
  [super viewDidLoad];
  
  NSURL *jsCodeLocation;

    #ifdef DEBUG
      jsCodeLocation = [NSURL URLWithString:@"http://192.168.121.40:8081/index.ios.bundle?platform=ios"];
    #else
     jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];
    #endif

  RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                      moduleName:@"eCourseApp"
                                               initialProperties:self.param
                                                   launchOptions:nil];
  rootView.backgroundColor = [UIColor whiteColor];
  self.view = rootView;


  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backToLogin) name:@"backToLogin" object:nil];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playViedo:) name:@"playViedo" object:nil];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveBridge:) name:@"saveBridge" object:nil];
  
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshTokenToRN) name:UIApplicationDidBecomeActiveNotification object:nil];
  
  [[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(ejectPayPanel:) name:@"ejectPayPanel" object:nil];
}

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  
  [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
  [super viewWillDisappear:animated];
  
  [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)playViedo:(NSNotification *)noti {
  
  NSString *videoUrl = [noti.userInfo objectForKey:@"playUrl"];
  if (videoUrl && videoUrl.length > 0) {
    
    JZEClassPlayerController *eClassPlayerController = [[JZEClassPlayerController alloc] init];
    eClassPlayerController.videoUrl = videoUrl;
    [self presentViewController:eClassPlayerController animated:YES completion:nil];
  }
  
}

- (void)ejectPayPanel:(NSNotification *)noti {
  NSString *money = [noti.userInfo objectForKey:@"money"];
  
  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
     [self.bridge.eventDispatcher sendDeviceEventWithName:@"PayPassWordSetted_NativeEmitter" body:@"123456adfd"];
  });
}


- (void)backToLogin {
  [self.navigationController popViewControllerAnimated:YES];
}

- (void)saveBridge:(NSNotification *)noti {
  
  self.bridge = [noti.userInfo objectForKey:@"bridge"];
  
}

- (void)refreshTokenToRN {
  [self.bridge.eventDispatcher sendDeviceEventWithName:@"TokenFreshed_NativeEmitter" body:@"123456adfd"];
}

- (void)dealloc {
  [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end

